/**
 * 
 */
package vn.edu.ctu.cit.se.android.karfinder;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Darren
 *
 */
public class PlaylistViewerActivity extends Activity{

	private SongListView songListView;
	private ListView listView;
	private TextView textView;
	private ArrayList<Song> songData;
	private DatabaseHelper dbHelper;
	private int playlistId;
	private String query;
	private int selected;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		
		dbHelper = new DatabaseHelper(this);
		setContentView(R.layout.layout_playlist_viewer);
		
		songListView = new SongListView(this, R.id.lvPlaylistViewer, "");
		playlistId = getIntent().getIntExtra("playlistId", 0);
		textView = (TextView)findViewById(R.id.tvPlaylistViewerName);
		textView.setText(getIntent().getStringExtra("playlistName"));
		songListView.setQuery("select a.SONG_ID, a.SONG_TITLE, a.SONG_LYRICS from SONG a, INCLUDE b " +
				"where b.PLAYLIST_ID = " + playlistId + " and b.SONG_ID = a.SONG_ID");
		
		registerForContextMenu(songListView.getListView());
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if(v.getId()==R.id.lvPlaylistViewer){
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
			//menu.setHeaderTitle("Playlist Song");
			String[] menuItemsAdd = getResources().getStringArray(R.array.songlistview_context_menu);
			String[] menuItemsDelete = getResources().getStringArray(R.array.contextmenu_playlist);
			menu.add(Menu.NONE, 0, 0, menuItemsAdd[0]);
			menu.add(Menu.NONE, 1, 1, menuItemsAdd[1]);
			menu.add(Menu.NONE, 2, 2, menuItemsDelete[1]);					
		}
	}

	
	@Override
	public boolean onContextItemSelected(MenuItem item) {		
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		int menuItemIndex = item.getItemId();
		//String[] menuItems = getResources().getStringArray(R.array.contextmenu_playlist);
		//String menuItemName = menuItems[menuItemIndex];
		
		//dbHelper = new DatabaseHelper(this);
		final int songId;
		//songData = dbHelper.searchList;
		songData = songListView.getSongData();
		songId = songData.get(info.position).getId();
		
		
		switch (menuItemIndex){
			case 0:
				dbHelper.setFavorite(songId + "", true);
				Toast.makeText(PlaylistViewerActivity.this, R.string.added_to_favorite,
							   Toast.LENGTH_SHORT).show();
				break;
			case 1:
				selected = -1;
				final ArrayList<Playlist> playlistList = dbHelper.fetchPlaylistList();
				final String[] playlistNames = new String[playlistList.size()];
				for (int i = 0; i < playlistList.size(); ++i)
					playlistNames[i] = playlistList.get(i).getName();
				AlertDialog.Builder playlistPicker = new AlertDialog.Builder(PlaylistViewerActivity.this);
				playlistPicker.setTitle(R.string.choose_a_playlist);
				playlistPicker.setSingleChoiceItems(playlistNames, -1,
						new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Log.i("playlist position", playlistNames[which]);
						selected = which;
					}
				});
				playlistPicker.setPositiveButton(R.string.add_to_playlist,
						new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Log.i("final position", selected + "");
						if (selected != -1) {
							long resultCode = dbHelper.addSongToPlaylist(playlistList.get(selected).getId(),
																		 songId);
							Toast.makeText(PlaylistViewerActivity.this,
										   ((resultCode != -1) ? R.string.added_to_playlist : R.string.added_to_playlist_already),
										   Toast.LENGTH_SHORT).show();
						}
					}
				});
				playlistPicker.setNegativeButton(R.string.dialog_button_playlist_cancel, null);
				playlistPicker.show();
				break;
			case 2:
				dbHelper.deleteSongOfPlaylist(playlistId, songId);		
				Toast.makeText(PlaylistViewerActivity.this,R.string.dialog_delete_song_successful, Toast.LENGTH_SHORT).show();
				songListView.setQuery("select a.SONG_ID, a.SONG_TITLE, a.SONG_LYRICS from SONG a, INCLUDE b " +
						"where b.PLAYLIST_ID = " + playlistId + " and b.SONG_ID = a.SONG_ID");
				break;
		}
		
				
		return true;
		
	}
	 
	
	
	
}